package top.lywivan.system.controller;

import top.lywivan.common.entity.Result;
import top.lywivan.common.pojo.Question;
import top.lywivan.system.service.SearchQuestionService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private SearchQuestionService searchQuestionService;

    @PostMapping("/add")
    @HystrixCommand(fallbackMethod = "fallbackMethod")
    public Result addQuestion(@RequestBody Question question){
        Result result = searchQuestionService.addQuestion(question);
        if (result.isFlag()){
            return new Result(true,"添加成功");
        }
        return new Result(false,"添加失败");
    }

    public Result fallbackMethod(Question question){
        return new Result(false,"响应时间过长，发送了熔断降级");
    }
}
